package cn.lsnu.common.sys.controller;

import cn.lsnu.common.interfacetools.interfacecontroller.BaseController;
import cn.lsnu.common.interfacetools.interfaceentity.Page;
import cn.lsnu.common.sys.entity.MenuEntity;
import cn.lsnu.common.sys.service.MenuService;
import cn.lsnu.utils.Global;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sys/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    @RequestMapping("/save")
    public void save(MenuEntity menuEntity) throws Exception{


        menuService.save(menuEntity);
    }

    @RequestMapping("/saveinter")
    public void saveInterfacer(MenuEntity menuEntity) throws Exception{
        menuService.saveInterfacer(menuEntity);
    }


    @RequestMapping("/list")
    public String list(@RequestParam("menuId")String id, Model model, HttpSession session,MenuEntity entity) throws Exception {
        //每个-list页面的id
        model.addAttribute("pageId",id);
        //获取页面的所有信息
        String userCode = (String) session.getAttribute(Global.SESSION_ID);
        entity.setUserCode(userCode);
        entity = menuService.getPageMenu(entity);
        model.addAttribute("pageMenuMsg",entity);
        return "/webpage/modules/oa/admin-permission";
    }

    @RequestMapping("/listjson")
    public  @ResponseBody  Page<MenuEntity> listJson(
            MenuEntity menuEntity) throws Exception {
       Page<MenuEntity> lists = menuService.findPage(menuEntity);
        return lists;
    }

    @RequestMapping("/listalljson")
    public  @ResponseBody  List<MenuEntity> findList(
            MenuEntity menuEntity) throws Exception {
        return  menuService.findList(menuEntity);
    }

    @RequestMapping("/listinter")
    public String listInterfacer(@RequestParam("menuId") String id,Model model, HttpSession session, MenuEntity entity) throws Exception {
        String firstTitle = entity.getFirstTitle();
        //每个-list页面的id
        model.addAttribute("pageId",id);
        //获取页面的所有信息
        String userCode = (String) session.getAttribute(Global.SESSION_ID);
        entity.setUserCode(userCode);
        entity = menuService.getPageMenu(entity);
        entity.setFirstTitle(firstTitle);
        entity.setSecondTitle("接口管理列表");
        model.addAttribute("pageMenuMsg",entity);
        return "/webpage/common/sys/sys_interfacer";
    }

    @RequestMapping("/listjsoninterfacer")
    public  @ResponseBody  Page<MenuEntity> listJsonInterfacer(
            MenuEntity menuEntity) throws Exception {
        Page<MenuEntity> lists = menuService.findPage(menuEntity);
        return lists;
    }



    @RequestMapping("/del")
    public @ResponseBody  Map<String,String> delete(MenuEntity menuEntity)throws  Exception{

        Map<String,String> msg = new  HashMap<String,String>();
        menuService.delete(menuEntity);
        msg.put("msg","success");
        return msg;
    }

    @RequestMapping("/updmsg")
    public String updateMsg(String id,Model model) throws  Exception{
        MenuEntity menuEntity =  menuService.get(id);
        model.addAttribute("updmenu",menuEntity);
        return "webpage/modules/oa/admin-permission-upd";
    }
    @RequestMapping("/upd")
    public void update(MenuEntity menuEntity) throws  Exception{
        menuService.update(menuEntity);
    }


    @RequestMapping("/updintermsg")
    public String updateInterMsg(String id,Model model) throws  Exception{
        MenuEntity menuEntity =  menuService.get(id);
        model.addAttribute("updmenu",menuEntity);
        return "webpage/common/sys/sys_interfacer_upd";
    }
    @RequestMapping("/updinter")
    public void updateInter(MenuEntity menuEntity) throws  Exception{
        menuService.updateInterfacer(menuEntity);
    }



}
